﻿// License: MS-PL
// Based heavily on demo code that is part of http://msdn.microsoft.com/en-us/magazine/dd419663.aspx .
// Has been modified to fit coding style of MachineQA project.

using System;
using System.Windows.Input;

namespace MachineQA.ViewModel
{
    /// <summary>
    /// This ViewModelBase subclass requests to be removed 
    /// from the UI when its CloseCommand executes.
    /// This class is abstract.
    /// </summary>
    public abstract class WorkspaceViewModel : ViewModelBase
    {
        RelayCommand _closeCommand;

        /// <summary>
        /// Default constructor for a WorkspaceViewModel.
        /// </summary>
        protected WorkspaceViewModel()
        {
        }

        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to remove this workspace from the user interface.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                    _closeCommand = new RelayCommand(param => this.OnRequestClose());

                return _closeCommand;
            }
        }

        /// <summary>
        /// Raised when this workspace should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        /// <summary>
        /// If the request closed event handler is set to a value it will be called during
        /// object closing.
        /// </summary>
        void OnRequestClose()
        {
            EventHandler handler = this.RequestClose;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }
    }
}
